<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
 *
 * @author  PyroCMS Dev Team
 * @package PyroCMS\Core\Modules\Blog\Controllers
 */
class Affiliate extends L_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->db->dbprefix="l_";
		$this->load->library('session');
		$this->load->model('affiliate_m');
	}

	public function initHtml(){
		$this->load->helper('url');
		$this->template
		->append_css('ui-lightness/jquery-ui-min.css')
		->append_css('module::affiliate.css')
		->append_css('ui.jqgrid.css')
		->append_css('960_24_col.css')
		->append_css('daterangepicker.css')
		->append_js('libs/jquery.blockUI.js')
		->append_js('libs/jquery-ui-min.js')
		->append_js('libs/grid.locale-en.js')
		->append_js('libs/jquery.jqGrid.js')
		->append_js('libs/date.js')
		->append_js('libs/daterangepicker.js')
		->append_js('module::affiliate.js');
	}
	/**
	 * Show all created blog posts
	 */

	public function index()
	{
		$this->initHtml();

		$model = new Affiliate_m();
		$affiliateId=$this->session->userdata('user_id');
		
		$this->template
		->set("balance",$model->getBalance($affiliateId))
		->build('index');
	}

	public function memberwl()
	{
		$this->initHtml();

		// Params
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
		$metaModel=array(
			"fromdate"=>$fromdate,
			"todate"=>$todate
		);

		$this->template
		->append_js('module::memberwl.js')
		->set("metaModel",$metaModel)
		->build('memberwl');
	}


	public function memberwlData()
	{
		// Params
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
		$affiliateId=$this->session->userdata('user_id');
		$model = new Affiliate_m();
		$memberWlList= $model->getMemberWl($fromdate, $todate, $affiliateId);

		$member_win_lose =0;
		$affiliate_win_lose=0;
		$affiliate_commission=0;
		$affiliate_total=0;
		foreach($memberWlList as $win_lose){
			$member_win_lose+=$win_lose->member_win_lose;
			$affiliate_commission+=$win_lose->affiliate_commission;
			$affiliate_win_lose+=$win_lose->affiliate_win_lose;
			$affiliate_total+=$win_lose->affiliate_total;
		}

		$result  = array(
			"Main"=>$memberWlList,
			"Total"=>array("affiliate_commission"=>$affiliate_commission,
			"member_win_lose"=>$member_win_lose,
			"affiliate_total"=>$affiliate_total,
			"affiliate_win_lose"=>$affiliate_win_lose)
		);

		echo json_encode($result);
	}

	public function paymenthistory()
	{
		$this->initHtml();

		// Params
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
		$metaModel=array(
			"fromdate"=>$fromdate,
			"todate"=>$todate
		);

		$this->template
		->append_js('module::payment.js')
		->set("metaModel",$metaModel)
		->build('paymenthistory');
	}


	public function paymenthistoryData()
	{
		// Params
		$fromdate=isset($_REQUEST['fromdate'])?date_create_from_format('Y-m-d',$_REQUEST['fromdate']):(new DateTime());
		$todate=isset($_REQUEST['todate'])?date_create_from_format('Y-m-d',$_REQUEST['todate']):(new DateTime());
		$affiliateId=$this->session->userdata('user_id');
		$model = new Affiliate_m();
		$paymentHistory= $model->getPaymentHistory($fromdate, $todate, $affiliateId);

		$result  = array(
			"Main"=>$paymentHistory,
			"Total"=>null
		);

		echo json_encode($result);
	}

}
